<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class ShopInfoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $shop = session()->get('shopInfo');
        $shopid=$shop->shop_id;
        $shopInfo = DB::table('shop')->where('shop_id',$shopid)->first();
        // dd($shopInfo);
        //dd($shopInfo->store_name);
        return view('home.shop_info')->with('shopInfo',$shopInfo);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //商家修改店铺信息
        // dd($request->file('image'));
        //dd($request->except('_token'));
        if($img=$request->file('image')){
           $ext = $img->getClientOriginalExtension(); //获取上传文件名的后缀名
           $path = public_path('upload/');
           $name = time().rand(10,100).'x.'.$ext;
           if($img->move($path,$name)){
               $url = 'upload/'.$name;
               // dd($url);
            }

        }else{
            $url = $request->photo;
            // dd($url);
        }

        //插入商家信息表
        $user = DB::table('shop')->where('shop_id',$id)->update([
           'store_name'       =>$request->store_name,
           'shop_description' =>$request->description,
           'shop_phone'       =>$request->phone,
           'shop_email'       =>$request->email,     
           'shop_logo'       =>$url     
        ]);

        return redirect('/shopInfo');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
